/**
 * Created by DoveXL on 2018/12/4.
 */
$(document).ready(function () {
    queryUser();
});
function UserChangeByUserType() {
    queryUser();
}


function queryUser() {

    var UserType = $('input[name="UserCategory"]:checked').val().trim();
    if(UserType=="Not")
    {
        UserType =null;
    }

    $.ajax({
        async: true,
        type: "post",
        url: "/VolunteerPlatform/AdminInfo/CertainAdminInfo",
        dataType: "json",
        contentType : 'application/json;charset=utf-8',
        data: JSON.stringify({page:'1',"user_flag":UserType,"pagesize":8}),
        cache: false,
        success: function(data) {
            var result = eval(data);
            $("#UserManagementInfo tr").empty();
            for (var i = 0; i<result.pagelist.length; i++) {
                var row = CreateAdminRow(result.pagelist[i]);
                $("#UserManagementInfo").append(row);
            };
            var currentPage = result.currentpage; //当前页数
            var pageCount = result.pagecount; //总页数
            var options = {
                bootstrapMajorVersion: 3, //版本
                currentPage: currentPage, //当前页数
                totalPages: pageCount, //总页数
                numberOfPages: 8,
                shouldShowPage:true,//是否显示该按钮
                itemTexts: function (type, page, current) {
                    switch (type) {
                        case "first":
                            return "首页";
                        case "prev":
                            return "上一页";
                        case "next":
                            return "下一页";
                        case "last":
                            return "末页";
                        case "page":
                            return page;
                    }
                },
                onPageClicked: function (event, originalEvent, type, page) {
                    $.ajax({
                        async: true,
                        url: "/VolunteerPlatform/AdminInfo/CertainAdminInfo",
                        type: "post",
                        dataType : "json",
                        contentType : 'application/json;charset=utf-8',
                        data: JSON.stringify({page:page,"user_flag":UserType,"pagesize":8}),
                        cache: false,
                        success: function (data) {
                            $("#UserManagementInfo tr").empty();
                            var result = eval(data);
                            for (var i = 0; i <result.pagelist.length; i++) {
                                var row = CreateAdminRow(result.pagelist[i]);
                                $("#UserManagementInfo").append(row);
                            };
                        }
                    });
                }
            };
            $('#UserpageLimit').bootstrapPaginator(options);
        }
    });
}


function CreateAdminRow(UserInfo) {
    var row = document.createElement('tr');
    var ChooseCell = document.createElement('td');
    var btnLabel = document.createElement('label');
    var btnCheckbox = document.createElement('input');
    btnCheckbox.setAttribute("type","radio");
    btnCheckbox.setAttribute("name","UserRadio");
    btnLabel.appendChild(btnCheckbox);
    ChooseCell.appendChild(btnLabel);
    row.appendChild(ChooseCell);
    var UserNameCell = document.createElement('td');
    UserNameCell.innerHTML = UserInfo.user_name;
    row.appendChild(UserNameCell);
    var UserRealNameCell = document.createElement('td');
    UserRealNameCell.innerHTML =  UserInfo.user_realname;
    row.appendChild(UserRealNameCell);
    var UserRoleCell = document.createElement('td');
    UserRoleCell.innerHTML = UserInfo.user_role;
    row.appendChild(UserRoleCell);
    var UserStatusCell = document.createElement('td');
    UserStatusCell.innerHTML = UserInfo.user_status;
    row.appendChild(UserStatusCell);
    return row;
}